using UnityEngine;
public class RoleInfo : MonoBehaviour
{
    public int hpMax = 30; // 最大生命值
    public int hp = 10; //生命值
    public HpBar hpBar; // 血条
    public Transform hpPoint; // 血条位置

    protected void Start()
    {
        if (hpPoint)
        {
            hpBar = HpMgr.ins.AddHp(hpPoint);
            hp = hpMax;
            setHp();
        }
    }

    /// <summary>受到伤害</summary>
    /// <param name="damage">伤害值</param>
    public void hurt(int damage)
    {
        setHp(-damage);
    }

    /// <summary>设置生命值</summary>
    /// <param name="num">数值可以是 正/负 数</param>
    public void setHp(int num = 0)
    {
        hp += num;  // 扣血
        if (hp < 0) hp = 0;
        if (hpBar) hpBar.SetHp(hpMax, hp);
        if (hp == 0)
        {
            gameObject.SendMessage("die"); //死亡
            Debug.Log(gameObject.name + "死亡");
        }
    }
}
